package cn.powerone.scm.service.impl;

import java.util.List;
import cn.powerone.common.utils.DateUtils;
import cn.powerone.scm.domain.ScmDefectInfo;
import cn.powerone.scm.service.IScmDefectInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.powerone.scm.mapper.ScmDefectTicketMapper;
import cn.powerone.scm.domain.ScmDefectTicket;
import cn.powerone.scm.service.IScmDefectTicketService;

/**
 * 售后工单Service业务层处理
 * 
 * @author PowerOne
 * @date 2025-08-22
 */
@Service
public class ScmDefectTicketServiceImpl implements IScmDefectTicketService 
{
    @Autowired
    private ScmDefectTicketMapper scmDefectTicketMapper;

    @Autowired
    private IScmDefectInfoService scmDefectInfoService;

    /**
     * 查询售后工单
     * 
     * @param id 售后工单主键
     * @return 售后工单
     */
    @Override
    public ScmDefectTicket selectScmDefectTicketById(Long id)
    {
        return scmDefectTicketMapper.selectScmDefectTicketById(id);
    }

    /**
     * 查询售后工单列表
     * 
     * @param scmDefectTicket 售后工单
     * @return 售后工单
     */
    @Override
    public List<ScmDefectTicket> selectScmDefectTicketList(ScmDefectTicket scmDefectTicket)
    {
        return scmDefectTicketMapper.selectScmDefectTicketList(scmDefectTicket);
    }

    /**
     * 新增售后工单
     * 
     * @param scmDefectTicket 售后工单
     * @return 结果
     */
    @Override
    public int insertScmDefectTicket(ScmDefectTicket scmDefectTicket)
    {
        scmDefectTicket.setCreateTime(DateUtils.getNowDate());
        return scmDefectTicketMapper.insertScmDefectTicket(scmDefectTicket);
    }

    /**
     * 修改售后工单
     * 
     * @param scmDefectTicket 售后工单
     * @return 结果
     */
    @Override
    public int updateScmDefectTicket(ScmDefectTicket scmDefectTicket)
    {
        scmDefectTicket.setUpdateTime(DateUtils.getNowDate());
        return scmDefectTicketMapper.updateScmDefectTicket(scmDefectTicket);
    }

    /**
     * 批量删除售后工单
     * 
     * @param ids 需要删除的售后工单主键
     * @return 结果
     */
    @Override
    public int deleteScmDefectTicketByIds(Long[] ids)
    {
        ScmDefectInfo condition = new ScmDefectInfo();
        for (Long id : ids){
            condition.setTicketId(id);
            List<ScmDefectInfo> list = scmDefectInfoService.selectScmDefectInfoList(condition);
            if (!list.isEmpty()){
                throw new RuntimeException("售后工单下有数据，请先删除数据");
            }
        }
        return scmDefectTicketMapper.deleteScmDefectTicketByIds(ids);
    }

    /**
     * 删除售后工单信息
     * 
     * @param id 售后工单主键
     * @return 结果
     */
    @Override
    public int deleteScmDefectTicketById(Long id)
    {
        return scmDefectTicketMapper.deleteScmDefectTicketById(id);
    }
}
